using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_addtags : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			if(Request.QueryString.Get("photo") != null) {
				ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("photo")));
				Account tmp = ap.GetAccountBySession(Session["Zappr"].ToString());
				Account acc = ap.GetAccount(item.UserID);

				if(item.UserID == tmp.ID) {
					foreach(TagItem tag in item.Tags) {
						if(txtTags.Text.Trim() != "") {
							txtTags.Text += " ";
						}

						txtTags.Text += tag.Tag;
					}

					foreach(TagItem tag in item.Tags) {
						HyperLink link = new HyperLink();
						link.CssClass = "sideLinks";
						link.NavigateUrl = "~/default.aspx?id=images&user="
							+ acc.Username
							+ "&tag=" + tag.Tag;
						link.Text = "&raquo; " + tag.Tag;

						plcTags.Controls.Add(link);

						Literal l = new Literal();
						l.Text = "<br />";

						plcTags.Controls.Add(l);
					}
				}
				else {
					Response.Redirect("~/default.aspx?item=" + Request.QueryString.Get("photo"));
				}
			}
			else {
				Response.Redirect("~/default.aspx?id=dashboard");
			}
		}
		else {
			Response.Redirect("~/default.aspx?id=dashboard");
		}
	}

	/// <summary>
	/// btnSave_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e) {
		int i = 0;
		ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("photo")));

		if(txtTags.Text.Trim() != "") {
			string[] tags = txtTags.Text.Split(new char[] { ';', ',', ' ' });

			item.Tags.Clear();

			for(i = 0; i < tags.Length; i++) {
				if(tags[i].Trim() != "") {
					TagItem tag = new TagItem();
					tag.ID = Guid.NewGuid().ToString();
					tag.Tag = tags[i];

					item.Tags.Add(tag);
				}
			}
		}

		ip.SaveImage(item);

		Response.Redirect("~/default.aspx?item=" + item.ImageID.ToString());
	}

	/// <summary>
	/// btnCancel_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e) {
		if(Request.QueryString.Get("Photo") != null) {
			ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("photo")));
			Account acc = ap.GetAccount(item.UserID);

			Response.Redirect("~/default.aspx?id=images&user=" + acc.Username);
		}
		else {
			Response.Redirect("~/default.aspx?id=dashboard");
		}
	}
}
